<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>

</body>
<script>
  // 浅拷贝
  let obj = {
    name: '张三',
    id: 1,
    msg: {
      age: 18
    }
  }
  let obj2 = {}
  for (let k in obj) {
    obj2[k] = obj[k]
  }
  console.log(obj);
  obj2.msg.age = 20
  console.log(obj2);
  // 浅拷贝只是拷贝一层，更深层的对象级别只会拷贝引用

  // 深拷贝
  let ject = {
    name: '张三',
    age: 18
  }

  // 先转为json格式字符串，再转回来
  let newObj = JSON.parse(JSON.stringify(ject));

  ject.age = 20;
  console.log(ject, newObj); // 输出18
</script>

</html>